using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using Xeneize.SolaraBL.CRM;

namespace WIntegrator.Pages
{
    public partial class CRMMain : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                FillBusinessTypeCombo();
            }
            ((WIntegrator.Controls.CRM.CRMNotes)CRMNotes1).CurrentCustomer = Convert.ToInt32(((HiddenField)this.leftPanel.FindControl("txtCompanyID")).Value);
            ((WIntegrator.Controls.CRM.CRMDocuments)CRMDocuments1).CurrentCustomer = Convert.ToInt32(((HiddenField)this.leftPanel.FindControl("txtCompanyID")).Value);
            ((WIntegrator.Controls.CRM.CRMSalesOrder)CRMSalesOrder1).CurrentCustomer = Convert.ToInt32(((HiddenField)this.leftPanel.FindControl("txtCompanyID")).Value);
            ((WIntegrator.Controls.CRM.CRMEstimates)CRMEstimates1).CurrentCustomer = Convert.ToInt32(((HiddenField)this.leftPanel.FindControl("txtCompanyID")).Value);
        }

        #region Private Methods

        private void FillBusinessTypeCombo()
        {
           RadComboBox tmp =  (RadComboBox)this.topContent.FindControl("cmbCompanyType");
           tmp.DataSource =  CRMBusinessTypeList.GetAll();
           tmp.DataTextField = "Description";
           tmp.DataValueField = "ID";
        }
 
  
        private void FillGrid()
        {            
            this.grdCustomers.DataSource = CRMCompanies.GetAll();            
        }
        #endregion 

        protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e)
        {
            switch (e.Item.Text.ToLower())
            {
                case "save":
                    {
                        SaveNewCompany();
                        this.leftPanel.FindControl("grdCustomers").DataBind();
                        break;
                    }
                case "add document":
                    {
                        CreateAddDocWindow();
                        break;
                    }
                case "create folder":
                    {
                        CreateFolderWindow();
                        break;
                    }

            }
        }

        /// <summary>
        /// Create New Window
        /// </summary>
        protected void CreateAddDocWindow()
        {
            RadWindow wCreateView = new RadWindow();
            wCreateView.NavigateUrl = "~/Pages/CRM/CRMUploadDocument.aspx?cc="+ this.txtCompanyID.Value;
            wCreateView.Modal = true;            
            wCreateView.Height = Unit.Pixel(350);
            wCreateView.Width = Unit.Pixel(400);
            wCreateView.VisibleOnPageLoad = true;
            this.RadWindowManager1.Windows.Add(wCreateView);
        }
        /// <summary>
        /// Create New Window
        /// </summary>
        protected void CreateFolderWindow()
        {
            RadWindow wCreateView = new RadWindow();
            wCreateView.NavigateUrl = "~/Pages/CRM/CRMCreateDocumentFolder.aspx?cc=" + this.txtCompanyID.Value;
            wCreateView.Modal = true;
            wCreateView.Height = Unit.Pixel(350);
            wCreateView.Width = Unit.Pixel(400);
            wCreateView.VisibleOnPageLoad = true;
            this.RadWindowManager1.Windows.Add(wCreateView);
        }

        /// <summary>
        /// Save new company
        /// </summary>
        private void SaveNewCompany()
        {
            CRMCompany tmpCompany=null;
            int currentCompanyID = Convert.ToInt32(this.txtCompanyID.Value);
            if (currentCompanyID == -1)
            {
                tmpCompany = new CRMCompany();
            }
            else
            {
                tmpCompany = new CRMCompany(currentCompanyID);
            }
            tmpCompany.CompanyName = ((RadTextBox)this.topContent.FindControl("txtCompanyName")).Text;
            tmpCompany.AddressBlock = ((RadTextBox)this.topContent.FindControl("txtCompanyAddress")).Text;
            tmpCompany.Phone = ((RadTextBox)this.topContent.FindControl("txtCompanyPhone")).Text;
            tmpCompany.Fax = ((RadTextBox)this.topContent.FindControl("txtCompanyFax")).Text;
            tmpCompany.WebSite = ((RadTextBox)this.topContent.FindControl("txtWebsite")).Text;
            tmpCompany.FtpSite = ((RadTextBox)this.topContent.FindControl("txtFtpSite")).Text;
            tmpCompany.SalesTerms = ((RadTextBox)this.topContent.FindControl("txtSalesTerms")).Text;
            tmpCompany.SalesRep = ((RadTextBox)this.topContent.FindControl("txtSalesRep")).Text;
            RadComboBox tmp = (RadComboBox)this.topContent.FindControl("cmbCompanyType");
            tmpCompany.BusinessType = new CRMBusinessType();//Convert.ToInt32(tmp.SelectedValue));
            //Office
            if (((RadComboBox)this.topContent.FindControl("cmbOffice")).Text != string.Empty)
            {                
                CRMOffice tmpOffice = null;
                if (((RadComboBox)this.topContent.FindControl("cmbOffice")).SelectedValue != string.Empty)
                {
                    tmpOffice = new CRMOffice(Convert.ToInt32(((RadComboBox)this.topContent.FindControl("cmbOffice")).SelectedValue));
                }
                else
                {
                    tmpOffice = new CRMOffice();
                }
                tmpOffice.CompanyID = tmpCompany.ID;
                tmpOffice.Description = ((RadComboBox)this.topContent.FindControl("cmbOffice")).Text;
                tmpOffice.StreetName = ((RadTextBox)this.topContent.FindControl("txtOfficeAddress")).Text;
                tmpOffice.City = ((RadTextBox)this.topContent.FindControl("txtOfficeCity")).Text;
                tmpOffice.State = ((RadTextBox)this.topContent.FindControl("txtOfficeState")).Text;
                tmpOffice.ZipCode = ((RadTextBox)this.topContent.FindControl("txtOfficeZipCode")).Text;
                tmpOffice.Phone = ((RadTextBox)this.topContent.FindControl("txtOfficePhone")).Text;
                if (tmpCompany.Office.Count == 0)
                {
                    tmpCompany.Office.Add(tmpOffice);
                }
                else
                {
                    for (int i = 0; i < tmpCompany.Office.Count; i++)
                    {
                        if (tmpCompany.Office[i].ID == tmpOffice.ID)
                        {
                            tmpCompany.Office[i] = tmpOffice;
                        }
                    }
                }
            
                //Contacts
                if (((RadComboBox)this.topContent.FindControl("cmbContact")).Text != string.Empty)
                {                   
                    CRMContact tmpContact = null;
                    if (((RadComboBox)this.topContent.FindControl("cmbContact")).SelectedValue !=string.Empty)
                    {
                        tmpContact = new CRMContact(Convert.ToInt32(((RadComboBox)this.topContent.FindControl("cmbContact")).SelectedValue));
                    }
                    else
                    {
                        tmpContact = new CRMContact();
                    }
                    tmpContact.OfficeID = tmpOffice.ID;
                    tmpContact.FirstName = string.Empty;
                    tmpContact.LastName = ((RadComboBox)this.topContent.FindControl("cmbContact")).Text;
                    tmpContact.DirectPhone = ((RadTextBox)this.topContent.FindControl("txtContactPhone")).Text;
                    tmpContact.PhoneExt = ((RadTextBox)this.topContent.FindControl("txtContactPhoneExt")).Text;
                    tmpContact.CellPhone = ((RadTextBox)this.topContent.FindControl("txtContactMobile")).Text;
                    tmpContact.Email = ((RadTextBox)this.topContent.FindControl("txtContactEmail")).Text;
                    if (tmpOffice.Contacts.Count == 0)
                    {
                        tmpOffice.Contacts.Add(tmpContact);
                    }
                    else
                    {
                        for (int i = 0; i < tmpOffice.Contacts.Count;i++ )
                        {
                            if (tmpOffice.Contacts[i].ID == tmpContact.ID)
                            {
                                tmpOffice.Contacts[i] = tmpContact;                                
                            }
                        }
                    }   
                }
            }
            if (tmpCompany.IsNew)
            {
                tmpCompany.Save();
            }
            else
            {
                tmpCompany.Update();
            }
            tmpCompany = null;
            //Refresh Grid
            ((RadGrid)this.leftPanel.FindControl("grdCustomers")).Rebind();
        }

        protected void grdCustomers_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            FillGrid();
        }
   
        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {            
            string userControlName = "~\\Controls\\CRM\\Atomic\\" + e.Tab.PageView.ID + ".ascx";
            Control userControl = Page.LoadControl(userControlName);
            userControl.ID = e.Tab.PageView.ID + "_userControl";
            switch (e.Tab.PageView.ID)
            {
                case "CRMSalesOrder":
                    {
                        ((WIntegrator.Controls.CRM.CRMSalesOrder)userControl).CurrentCustomer = Convert.ToInt32(((HiddenField)this.leftPanel.FindControl("txtCompanyID")).Value);
                        break;
                    }
                case "CRMNotes":
                    {
                        ((WIntegrator.Controls.CRM.CRMNotes)userControl).CurrentCustomer = Convert.ToInt32(((HiddenField)this.leftPanel.FindControl("txtCompanyID")).Value); 
                        break;
                    }
                case "CRMDocuments":
                    {
                        ((WIntegrator.Controls.CRM.CRMDocuments)userControl).CurrentCustomer = Convert.ToInt32(((HiddenField)this.leftPanel.FindControl("txtCompanyID")).Value);
                        break;
                    }
            }
            e.Tab.PageView.Controls.Clear();
            e.Tab.PageView.Controls.Add(userControl);                    
        }

        protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
        {
            switch (TabContainer1.ActiveTab.ID.ToLower())
            {
                case "tabnote":
                    {
                        //this.CRMNotes1.FindControl("GridNotes").Rebind();
                        break;
                    }
                case "tabestimates":
                    {
                        //this.CRMSalesOrder1.FindControl("GridSalesOrder").Rebind();
                        break;
                    }

                case "tabsalesorders":
                    {
                        ((RadGrid)this.CRMSalesOrder1.FindControl("GridSalesOrder")).Rebind();
                        break;
                    }
                case "tabinvoices":
                    {
                        //this.CRMSalesOrder1.FindControl("GridInvoices").DataBind();
                        break;
                    }
                case "tabdocuments":
                    {
                        //this.CRMDocuments1.FindControl("GridDocuments").Rebind();
                        break;
                    }
            }
        }

    }
}
